<template>
  <div class="notification-card" :class="{ 'unread': !notification.isRead }">
    <div class="notification-icon">
      <i :class="getNotificationIcon(notification.notificationType)"></i>
    </div>
    <div class="notification-content">
      <div class="notification-header">
        <h4 class="notification-title">{{ notification.title }}</h4>
        <span class="notification-time">{{ formatTime(notification.createdAt) }}</span>
      </div>
      <p class="notification-text">{{ notification.content }}</p>
      <div class="notification-meta">
        <span class="notification-type">{{ getNotificationTypeText(notification.notificationType) }}</span>
        <span v-if="!notification.isRead" class="unread-badge">未读</span>
      </div>
    </div>
    <div class="notification-actions">
      <el-button 
        v-if="!notification.isRead" 
        type="text" 
        size="small" 
        @click="$emit('mark-read', notification)"
      >
        标记已读
      </el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "NotificationCard",
  props: {
    notification: {
      type: Object,
      required: true
    }
  },
  methods: {
    getNotificationIcon(type) {
      const icons = {
        'system': 'iconfont icon-xitong',
        'member_upgrade': 'iconfont icon-huiyuan',
        'points_change': 'iconfont icon-jifen',
        'promotion': 'iconfont icon-youhui',
        'LEVEL_UP': 'iconfont icon-huiyuan',
        'POINTS_EARNED': 'iconfont icon-jifen',
        'BENEFIT_UNLOCKED': 'iconfont icon-youhui'
      };
      return icons[type] || 'iconfont icon-xiaoxi';
    },
    
    getNotificationTypeText(type) {
      const types = {
        'system': '系统通知',
        'member_upgrade': '会员升级',
        'points_change': '积分变动',
        'promotion': '优惠活动',
        'LEVEL_UP': '等级升级',
        'POINTS_EARNED': '积分获得',
        'BENEFIT_UNLOCKED': '权益解锁'
      };
      return types[type] || '通知';
    },
    
    formatTime(time) {
      if (!time) return '';
      const date = new Date(time);
      const now = new Date();
      const diff = now - date;
      
      if (diff < 60000) { // 1分钟内
        return '刚刚';
      } else if (diff < 3600000) { // 1小时内
        return Math.floor(diff / 60000) + '分钟前';
      } else if (diff < 86400000) { // 1天内
        return Math.floor(diff / 3600000) + '小时前';
      } else if (diff < 604800000) { // 1周内
        return Math.floor(diff / 86400000) + '天前';
      } else {
        return date.toLocaleDateString();
      }
    }
  }
};
</script>

<style scoped>
.notification-card {
  display: flex;
  align-items: flex-start;
  padding: 16px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border-left: 4px solid transparent;
  margin-bottom: 12px;
}

.notification-card:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

.notification-card.unread {
  border-left-color: #667eea;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.03) 0%, rgba(118, 75, 162, 0.03) 100%);
}

.notification-icon {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
}

.notification-icon i {
  font-size: 18px;
  color: white;
}

.notification-content {
  flex: 1;
  min-width: 0;
}

.notification-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.notification-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0;
  line-height: 1.4;
}

.notification-time {
  font-size: 12px;
  color: #999;
  white-space: nowrap;
}

.notification-text {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  margin: 0 0 8px 0;
}

.notification-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

.notification-type {
  background: #667eea;
  color: white;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 500;
}

.unread-badge {
  background: #ff4757;
  color: white;
  padding: 2px 6px;
  border-radius: 8px;
  font-size: 10px;
  font-weight: 600;
}

.notification-actions {
  flex-shrink: 0;
  margin-left: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .notification-card {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .notification-icon {
    margin-right: 0;
    margin-bottom: 8px;
  }
  
  .notification-actions {
    margin-left: 0;
    margin-top: 8px;
  }
}
</style> 